/*
 * Created on 2005/05/10
 * Author aki@www.xucker.jpn.org
 * License Apache2.0 or Common Public License
 */
package jp.sourceforge.akjrcp.ui.draw2d;

import java.awt.Dimension;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.IFigure;

/**
 * keep horizontal and vartical scale
 *
 */
public class DoubleScalableFigure extends Figure implements DoubleScalable{

    protected double hzoom=1;
    protected double wzoom=1;
    
    protected int defaultWidth;
    protected int defaultHeight;
    
    public DoubleScalableFigure(int defaultWidth,int defaultHeight){
        this.defaultWidth=defaultWidth;
        this.defaultHeight=defaultHeight;
        setSize(defaultWidth,defaultHeight);
    }
    
    public void setScale(double v){
       setScale(v,v);
    }
    
    public double getHorizontalScale(){
        return wzoom;
    }
    public double getVerticalScale(){
        return hzoom;
    }
    
    public void setScale(double w,double h){
        hzoom=h;
        wzoom=w;
        resize();
        
        //List list=this.getChildren();
        IFigure[] figures=null;
	    //synchronized(this){//TODO back
	        figures=(IFigure[]) getChildren().toArray(new IFigure[getChildren().size()]);
	   // }
	    //log.infosize:"+figures.length);
	    
        for(int i=0;i<figures.length;i++){
            if(figures[i] instanceof DoubleScalable){
                ((DoubleScalable)figures[i]).setScale(w,h);
            }
        
        }
    }
    
    
    
    public void resize(){
        int w=0;
        w=(int)((double)defaultWidth*wzoom);
        int v=0;
        v=(int)((double)defaultHeight*hzoom);
       // log.inforesized:"+defaultWidth+"*"+wzoom+","+defaultHeight+"*"+hzoom+" ="+w+"x"+v);
        setSize(w,v);//call getParent().revalidate() manually
    }
    
}
